/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.zzz.view.managerControl;

import javax.swing.*;

import org.zzz.controller.CourseController;
import org.zzz.controller.TeacherCourseController;
import org.zzz.controller.UserController;
import org.zzz.entity.Course;
import org.zzz.entity.TeacherCourse;
import org.zzz.entity.TeacherVo;
import org.zzz.utils.Result;
import org.zzz.utils.ResultEnum;
import org.zzz.view.util.UiUtil;

import java.util.List;
import java.util.UUID;

/**
 *
 * @author ASUS
 */
public class managerTeaAlter extends javax.swing.JFrame {

    /**
     * Creates new form managerTeaAlter
     */
    public managerTeaAlter(String num,String name,String sex,String degree,String teaCou) {
        initComponents();
        init(num,name,sex,degree,teaCou);         //初始化
    }

    private void init(String num,String name,String sex,String degree,String teaCou) {
        this.setTitle("教师信息修改");
        UiUtil.setFrameCenter(this);
        this.setResizable(false);
        this.m_Nums=num;
        this.m_Name=name;
        this.m_Sex=sex;
        this.m_Degree=degree;
        this.m_TeaCourse=teaCou;
        this.temp_Nums = this.m_Nums;
        //设置工号不可修改
        this.gonghao.setText(this.m_Nums);
        this.gonghao.setEditable(false);

        //获取到现存的所有课程名，填入到“课程”下拉框内
        CourseController courseController = new CourseController();
        List<Course> data = courseController.listCourse().getData();
        int index=0;
        for (Course datum : data) {
            this.jComboBox5.insertItemAt(datum, index++);
        }
        this.xingming.setText(this.m_Name);

        if(this.m_Sex.equals("男")){
            this.jRadioButton1.setSelected(true);
        }else{
            this.jRadioButton2.setSelected(true);
        }

        //设置学历
        for(int i=0;i<this.jComboBox2.getItemCount();i+=1){
            if(this.jComboBox2.getItemAt(i).equals(this.m_Degree)){
                this.jComboBox2.setSelectedIndex(i);
                break;
            }
        }

        //设置课程
        for(int i=0;i<this.jComboBox5.getItemCount();i+=1){
            if(this.jComboBox5.getItemAt(i).getName().equals(this.m_TeaCourse)){
                this.jComboBox5.setSelectedIndex(i);
                break;
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        buttonGroup1 = new ButtonGroup();
        jComboBox2 = new javax.swing.JComboBox<>();
        jComboBox5 = new javax.swing.JComboBox<>();
        jLabel4 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        gonghao = new javax.swing.JTextField();
        xingming = new javax.swing.JTextField();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "专科", "本科", "硕士", "博士", "副教授", "教授"}));

        jComboBox5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox5ActionPerformed(evt);
            }
        });

        jLabel4.setText("工号");

        jButton1.setText("确定");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel5.setText("姓名");

        jButton2.setText("取消");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel6.setText("性别");

        jLabel7.setText("学历");

        jLabel8.setText("任课");

        jRadioButton1.setText("男");
        buttonGroup1.add(jRadioButton1);
        jRadioButton2.setText("女");
        buttonGroup1.add(jRadioButton2);
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(41, 41, 41)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel5)
                            .addGap(32, 32, 32)
                            .addComponent(xingming, javax.swing.GroupLayout.PREFERRED_SIZE, 252, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel4)
                            .addGap(32, 32, 32)
                            .addComponent(gonghao, javax.swing.GroupLayout.PREFERRED_SIZE, 252, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8))
                        .addGap(32, 32, 32)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jRadioButton1)
                                .addGap(18, 18, 18)
                                .addComponent(jRadioButton2))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jComboBox5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jButton1)
                                    .addComponent(jButton2))))))
                .addGap(28, 28, 28))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(gonghao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(xingming, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jRadioButton1)
                    .addComponent(jRadioButton2))
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(25, 25, 25)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(jComboBox5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(25, 25, 25)
                        .addComponent(jButton2)))
                .addContainerGap(43, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox5ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox5ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        //将信息写入数据库
        if(this.jRadioButton1.isSelected()){
            this.m_Sex="男";
        }else{
            this.m_Sex="女";
        }
        UserController userController = new UserController();
        TeacherCourseController teacherCourseController = new TeacherCourseController();
        this.m_Name=this.xingming.getText();
        this.m_Degree=this.jComboBox2.getSelectedItem().toString();
        this.m_TeaCourse=this.jComboBox5.getSelectedItem().toString();
//        System.out.println(this.m_Degree+this.m_TeaCourse);
        userController.updateTeacher(new TeacherVo(this.m_Nums,this.m_Name,this.m_Sex,this.m_Degree));
        userController.updateUserById(this.temp_Nums,this.m_Nums);
        Result<TeacherCourse> teacherCourseResult = teacherCourseController.getTCByTid(this.m_Nums);
        System.out.println(teacherCourseResult);
        if(teacherCourseResult.getStatus()== ResultEnum.SUCCESS.getStatus()){
            //存在工号和课程关系，获取id，修改
            System.out.println("修改");
            TeacherCourse data = teacherCourseResult.getData();
            teacherCourseController.updateTC(new TeacherCourse(data.getId(),
                            this.m_Nums,((Course)this.jComboBox5.getSelectedItem()).getId()));
        }else{
            System.out.println("添加");
            //不存在工号和课程关系，添加
            teacherCourseController.insertTC(new TeacherCourse(UUID.randomUUID().toString(),
                    this.m_Nums,((Course)this.jComboBox5.getSelectedItem()).getId()));
        }
        JOptionPane.showMessageDialog(null, "修改成功！");

        //回到上一个界面
        managerTeaSelect mts = new managerTeaSelect();
        this.dispose();
        mts.setVisible(true);


        //是否继续修改
//        if (JOptionPane.showConfirmDialog(null, "是否修改？", "Message", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
//            //是：将文本框清空
//            this.gonghao.setText("");
//            this.xingming.setText("");
//        } else {
//            //回到上一个界面
//            managerTeaSelect mts = new managerTeaSelect();
//            this.dispose();
//            mts.setVisible(true);
//        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        managerTeaSelect mts = new managerTeaSelect();
        this.dispose();
        mts.setVisible(true);
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(managerTeaAlter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(managerTeaAlter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(managerTeaAlter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(managerTeaAlter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new managerTeaAlter("","","","","").setVisible(true);
            }
        });
    }

    private String m_Nums;
    private String m_Name;
    private String m_Sex;
    private String m_Degree;
    private String m_TeaCourse;
    private String temp_Nums;


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JTextField gonghao;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox<String> jComboBox2;
    private javax.swing.JComboBox<Course> jComboBox5;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JTextField xingming;
    // End of variables declaration//GEN-END:variables
}
